#include "primaryscene.h"

PrimaryScene::PrimaryScene(QWidget *parent) : QMainWindow(parent)
{
    //设置窗口
    //固定尺寸
    this->setFixedSize(mainSceneWidth,mainSceneHeight);
    //标题
    this->setWindowTitle(mainSceneName);
    //图标
    this->setWindowIcon(QIcon(":/res/Coin0001.png"));

    //创建菜单
    QMenuBar * bar = menuBar();
    this->setMenuBar(bar);
    QMenu* startMenu = bar->addMenu("开始");
    QAction * quitAction = startMenu->addAction("退出");

    connect(quitAction,&QAction::triggered,[=](){
        this->close();
    });

    //返回按钮
    PushButton_01 * back =new PushButton_01(":/res/BackButton.png",":/res/BackButtonSelected.png");
    back->setParent(this);
    back->move(this->width() - back->width(),this->height() - back->height());

    connect(back,&PushButton_01::clicked,[=](){

        QTimer::singleShot(500,this,[=](){
            emit this->PrimarySceneBack();
        });

    });

    //创建关卡按钮
        for(int i = 0 ; i < 8;i++)
        {
            PushButton_01 * menuBtn = new PushButton_01(":/res/LevelIcon.png");
            menuBtn->setParent(this);
            //menuBtn->setFixedSize(menuBtn->width()*10,menuBtn->height()*10);
            menuBtn->move(100 + (i%4)*240 , 400+ (i/4)*300);

            //按钮上显示的文字
            QLabel * label = new QLabel;
            label->setParent(this);
            label->setFixedSize(menuBtn->width(),menuBtn->height());
            label->setText(QString::number(i+1));
            label->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter); //设置居中
            label->move(100 + (i%4)*240 , 400+ (i/4)*300);
            label->setAttribute(Qt::WA_TransparentForMouseEvents,true);  //鼠标事件穿透
            connect(menuBtn,&PushButton_01::clicked,[=](){
                this->pplay = new PrimaryPlay(i+1);
                this->pplay->show();
                this->hide();
                //监听pplay的返回信号
                connect(this->pplay,&PrimaryPlay::PrimarySceneBack,[=](){
                    this->show();
                    this->pplay->hide();
                    delete this->pplay;
                    this->pplay=NULL;
                });
            });
        }

}
//绘图事件
void PrimaryScene::paintEvent(QPaintEvent *){
    QPainter painter(this);
    QPixmap pix;
    pix.load(":/res/bfda0be809541c57e1407952d1e6042.jpg");
    painter.drawPixmap(0,0,this->width(),this->height(),pix);

    //标题
    pix.load(":/res/Title.png");
    painter.drawPixmap((this->width()-pix.width())*0.5,30,pix);
}
